#include<stdio.h>
#include <string.h>
#include<iostream>
#include "MyString.h"

int LyfStrLen(const char* pStr)
{
    if (NULL == pStr)
    {
        return 0;
    }

    int nLen = 0;
    while ('\0' != pStr[nLen++]);

    return nLen;
}

int LyfStrCpy(char* pstrDest, const char* pStrSrc)
{
    if (NULL == pstrDest)
    {
        return 1;
    }

	memset(pstrDest, 0, LyfStrLen(pStrSrc));
	memcpy(pstrDest, pStrSrc, LyfStrLen(pStrSrc));

    return 0;
}

int LyfStrCmp(const char* pstrDest, const char* pStrSrc)
{
    int nDest = LyfStrLen(pstrDest);
    int nSrc = LyfStrLen(pStrSrc);
    
    if (nDest != nSrc)
    {
        return nDest - nSrc;
    }

    for (int i = 0; i < nDest; i++)
    {
        if (pstrDest[i] != pstrDest[i])
        {
            return 1;
        }
    }

    return 0;
}

char* LyfStrCat(const char* pStrDest, const char* pStrSrc)
{
    int nDestLen = LyfStrLen(pStrDest);
    int nSrcLen = LyfStrLen(pStrSrc);
    char* pNew = NULL;
    if (0 == nDestLen)
    {
        pNew = new char[nSrcLen];
        memcpy(pNew, pStrSrc, nSrcLen);

        return pNew;
    }
    
    if (0 == nSrcLen)
    {
        pNew = new char[nDestLen];
        memcpy(pNew, pStrDest, nDestLen);
    }
    else
    {
        pNew = new char[nDestLen + nSrcLen - 1];
        memcpy(pNew, pStrDest, nDestLen - 1);
		memcpy(pNew + nDestLen - 1, pStrSrc, nSrcLen);
    }

    return pNew;
}
